//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartRangeBarView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartRangeBarView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartRangeBarView

IMPLEMENT_DYNCREATE(CChartRangeBarView, CBCGPChartExampleView)

CChartRangeBarView::CChartRangeBarView()
	: CBCGPChartExampleView(CChartRangeBarView::IDD)
{
	//{{AFX_DATA_INIT(CChartRangeBarView)
	m_nFillGradientType = 0;
	m_bDisplayShadow = TRUE;
	//}}AFX_DATA_INIT
}

CChartRangeBarView::~CChartRangeBarView()
{
}

void CChartRangeBarView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartRangeBarView)
	DDX_Control(pDX, IDC_SERIES_SHADOW, m_wndDisplayShadow);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_CBIndex(pDX, IDC_FILL_GRADIENT_TYPE, m_nFillGradientType);
	DDX_Check(pDX, IDC_SERIES_SHADOW, m_bDisplayShadow);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartRangeBarView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartRangeBarView)
	ON_CBN_SELENDOK(IDC_FILL_GRADIENT_TYPE, OnUpdateChart)
	ON_BN_CLICKED(IDC_SERIES_SHADOW, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartRangeBarView diagnostics

#ifdef _DEBUG
void CChartRangeBarView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartRangeBarView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartRangeBarView message handlers

void CChartRangeBarView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartType(m_Feature == CDemoFeature::BCGP_RangeBar ? BCGPChartBar : BCGPChartColumn, BCGP_CT_RANGE);

	CBCGPChartBarSeries* pSeries1 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("Series 1")));
	CBCGPChartBarSeries* pSeries2 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("Series 2")));

	pSeries1->SetCustomOffsetPercent(100);
	pSeries2->SetCustomOffsetPercent(100);

	pSeries1->SetCustomSizePercent(50);
	pSeries2->SetCustomSizePercent(50);

	pChart->AddChartDataYXY1(0, 1, 2, 0);
	pChart->AddChartDataYXY1(2, 3, 2, 0);
	pChart->AddChartDataYXY1(6, 8, 2, 0);
	pChart->AddChartDataYXY1(8, 7, 2, 0);
	pChart->AddChartDataYXY1(8, 1, 2, 0);
	pChart->AddChartDataYXY1(8, 4, 6, 0);
	pChart->AddChartDataYXY1(14, 1, 3, 0);
	pChart->AddChartDataYXY1(15, 8, 1, 0);

	pChart->AddChartDataYXY1(6, 4, 2, 1);
	pChart->AddChartDataYXY1(7, 2, 1, 1);
	pChart->AddChartDataYXY1(11, 3, 2, 1);
	pChart->AddChartDataYXY1(13, 7, 2, 1);
	pChart->AddChartDataYXY1(12, 1, 2, 1);
	pChart->AddChartDataYXY1(14, 5, 4, 1);
	pChart->AddChartDataYXY1(17, 1, 3, 1);
	pChart->AddChartDataYXY1(10, 8, 1, 1);

	if (m_wndChart.GetGraphicsManager()->GetType() == CBCGPGraphicsManager::BCGP_GRAPHICS_MANAGER_GDI)
	{
		m_wndDisplayShadow.EnableWindow(FALSE);
		m_wndDisplayShadow.ShowWindow(SW_HIDE);
		
		m_bDisplayShadow = FALSE;
	}

	OnUpdateChart();
}

void CChartRangeBarView::OnUpdateChart() 
{
	UpdateData();

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	CBCGPBrush::BCGP_GRADIENT_TYPE type;

	switch (m_nFillGradientType)
	{
	case 0:
	default:
		type = (CBCGPBrush::BCGP_GRADIENT_TYPE)-1;
		break;

	case 1:
		type = CBCGPBrush::BCGP_NO_GRADIENT;
		break;

	case 2:
		type = CBCGPBrush::BCGP_GRADIENT_HORIZONTAL;
		break;

	case 3:
		type = CBCGPBrush::BCGP_GRADIENT_VERTICAL;
		break;

	case 4:
		type = CBCGPBrush::BCGP_GRADIENT_BEVEL;
		break;

	case 5:
		type = pChart->GetChartCategory() == BCGPChartBar ? 
			CBCGPBrush::BCGP_GRADIENT_PIPE_HORIZONTAL : CBCGPBrush::BCGP_GRADIENT_PIPE_VERTICAL;
		break;
	}

	for (int i = 0; i < pChart->GetSeriesCount(); i++)
	{
		CBCGPChartSeries* pSeries = pChart->GetSeries(i);
		ASSERT_VALID(pSeries);

		pSeries->SetDefaultFillGradientType(type);
	}
	
	pChart->SetSeriesShadow(m_bDisplayShadow);
	pChart->Redraw();
}
